<?php

class Users  extends BaseUsers
{   
	/**
	* Class constructor
	* @return void
	*/
    public function __construct()
    {
        parent::__construct();
    }
        
    // Check login
    function isAuthenticated()
    {                
        $auth = Zend_Auth::getInstance();
        $storage = new Zend_Auth_Storage_Cookie();
        
        $auth->setStorage($storage);    
                  
        if($auth->hasIdentity())
        {      
           // check user exits in database
           $curr_user = $this->getCurrentUserInfor(); 
                                                                       
           $this->getId($curr_user->id);
           
           //Lib::pre($this); 
           if($this->getId())
           {
                return true;
           }
           else
           {
               $this->logout();
               return false;
           }
        }
        return false;
    }  
    
    
    // Get information of user 
    function getCurrentUserInfor(){
        $auth = Zend_Auth::getInstance(); 
        if($auth->getStorage()->isEmpty()){ 
            //print_r($auth->getStorage());exit;
            $user_info = $auth->getStorage()->value;
        } 
        else
            $user_info =  $auth->getStorage()->read() ;
        
        if(!$user_info)
            return array();
        
        $this->getById($user_info->id);
        
          
        if($this->getId())
            foreach($this->toArray() as $property=>$value)
            {    
                $user_info->$property = $value;                
            }
        
        return $user_info;         
    } 
    
    // user login with username and password
    function login($user_login,$password){
        $auth = Zend_Auth::getInstance();
        
        $authAdapter = new Zend_Auth_Adapter_DbTable($this->getAdapter());
        $authAdapter->setTableName('users')
                    ->setIdentityColumn('username')
                    ->setCredentialColumn('password')
                    ->setCredentialTreatment('MD5(?) and status = 1 ');                
        
        // Set the input credential values (e.g., from a login form)
        $authAdapter->setIdentity($user_login)
                    ->setCredential($password); 
        
        // Perform the authentication query, saving the result
        $result = $auth->authenticate($authAdapter);        
     
        if ($result->isValid()) {       
            // store the identity as an object where only the username and real_name have been returned            
            $auth_user = array('id');
            $auth->getStorage()->write($authAdapter->getResultRowObject($auth_user));         
            return true;
        } else {                                
            return false;
        }          
    }
    
    // Logout
    function logout() {
        $auth = Zend_Auth::getInstance();          
        $auth->clearIdentity();
        $auth->getStorage()->clear();                             
    }
       
}